001    package net.sf.xdc.processing;
002    
003    /*
004     *  Copyright 2005-2006 Jens Voß.
005     *
006     *  Licensed under the GNU Lesser General Public License (the "License");
007     *  you may not use this file except in compliance with the License.
008     *  You may obtain a copy of the License at
009     *
010     *       http://opensource.org/licenses/lgpl-license.php
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    import java.io.File;
020    import java.io.FileFilter;
021    import java.util.Set;
022    
023    import net.sf.xdc.util.IOUtils;
024    
025    /**
026     * The <code>FileSelector</code> class allows to specify which files within a
027     * directory are to be included in the XDC processing.
028     *
029     * @author Jens Voß
030     * @since 0.5
031     * @version 0.5
032     */
033    public class FileSelector {
034    
035      private class XdcFileFilter implements FileFilter {
036        public boolean accept(File file) {
037          String name = file.getName();
038          if (defaultExcludes && IOUtils.isDefaultExclude(name)) {
039            return false;
040          }
041          if (file.isDirectory()) {
042            return true;
043          }
044          String ext = name.substring(name.lastIndexOf('.') + 1);
045          return extensions.contains(ext);
046        }
047      } // end private class XdcFileFilter
048    
049      private File dir;
050      private Set extensions;
051      private boolean defaultExcludes;
052    
053      /**
054       * Public constructor.
055       *
056       * @param dir The current directory of this <code>FileSelector</code>
057       * @param extensions The set of file extensions for this
058       *         <code>FileSelector</code>
059       * @param defaultExcludes If this is <b>true</b>, certain files like version
060       *         control files are excluded by default.
061    
062       */
063      FileSelector(File dir, Set extensions, boolean defaultExcludes) {
064        this.dir = dir;
065        this.extensions = extensions;
066        this.defaultExcludes = defaultExcludes;
067      }
068    
069      /**
070       * Getter method for this <code>FileSelector</code>'s directory.
071       *
072       * @return The current directory of this <code>FileSelector</code>
073       */
074      public File getDir() {
075        return dir;
076      }
077    
078      /**
079       * This method retrieves the collection of all selected files within this
080       * <code>FileSelector</code>'s directory.
081       *
082       * @return An array of files and directories in this <code>FileSelector</code>'s
083       *          directory to which the XDC tool is to be applied
084       */
085      protected File[] selectFiles() {
086        return dir.listFiles(new XdcFileFilter());
087      }
088    
089      /**
090       * This method constructs a <code>FileSelector</code> for a subdirectory of
091       * the current directory.
092       *
093       * @param subdirName The name of the subdirectory for which a new
094       *         <code>FileSelector</code> is to be constructed
095       * @return A new <code>FileSelector</code> whose directory is the subdirectory
096       *          with the specified name
097       */
098      protected FileSelector moveToSubdir(String subdirName) {
099        return new FileSelector(new File(dir, subdirName), extensions, defaultExcludes);
100      }
101    
102    }